<?php

require_once 'session/access.php';

//if (!isAuthorized("utility.php")) {
//    header("location: notAuthorized.php?page=utility.php");
//}
$webmasterEmail = "nathaniel.stevens@navy.mil";

$jsFilenameArray = array("js/formValidation.js",
    "js/addRowToTable.js",
    "js/populateDropdown.js",
    "js/javascriptFunctions.js",
    "http://code.jquery.com/jquery-1.9.1.js",
    "http://code.jquery.com/ui/1.10.3/jquery-ui.js");

$cssFilenameArray = array("css/default.css", "http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css");

$projectStatusArray = array(
    "created" => "Initial Proposal Created",
    "taskSubmit" => "Task(s) created",
    "needTransition" => "Please submit a transition plan",
    "subtaskSubmit" => "Subtask(s) created",
    "submitComplete" => "PI Submitted for review",
    "piFeedback" => "Reviewer is requesting feedback from the PI",
    "piApproverSubmit" => "Proposal submitted for Approval/Disapproval",
    "approved" => "Proposal has been Approved. See Approved Cost",
    "disapproved" => "Proposal has been disapproved. See notes",
    "approvalFinal" => "Approval is finalized, project may begin",
    "rebaselineRequest" => "Rebaseline Request has been sent. Awaiting approval.",
    "rebaselineRequestApproved" => "Rebaseline request has been approved",
    "rebaselineRequestDisapproved" => "Rebaseline request has been disapproved",
    "rebaselineSubmitRequest" => "Rebaseline Submission has been sent. Awaiting approval.",
    "rebaselineSubmitApproved" => "Rebaseline Submission has been approved",
    "rebaselineSubmitDisapproved" => "Rebaseline Submission has been disapproved",
    "nonlaborRequired"=>"Please set Travel/Per Diem and Materials Costs");

$projectButtonNewStatusArray = array(
    "Finalize Approval" => "approvalFinal",
    "Disapprove" => "disapproved",
    "Approve" => "approved",
    "Submit feedback request to PI" => "piFeedback",
    "Submit to PI Approver" => "piApproverSubmit",
    "Submit Project For Review" => "submitComplete",
    "Request Rebaseline" => "rebaselineRequest",
    "Start Rebaseline" => "startRebaseline",
    "View Rebaseline Submission" => "viewRebaseline",
    "Recall Proposal Submission" => "subtaskSubmit");

$taskStatusArray = array("started" => "Started",
    "notStarted" => "Not Started",
    "pending" => "Pending",
    "completed" => "Completed");

$taskStatusColorArray = array("notStarted" => "#bb0000", //red
    "started" => "#00CC33", //green
    "pending" => "#e6e600", //yellow
    "completed" => "#66CCFF"); //light blue

$nrlAddressArray = array("NRL Stennis" => "7 Grace Hopper Ave Monterey, CA 93943-5502");

$activeProjectStatusArray = array("approved", "rebaselineRequest", "approvalFinal", "rebaselineRequestApproved", "rebaselineRequestDisapproved",
    "rebaselineSubmitRequest", "rebaselineSubmitApproved", "rebaselineSubmitDisapproved");

function getWorkSecurityArray(){
    $array = array();
    array_push($array, "UNCLASS");
    array_push($array, "SECRET");
    array_push($array, "TS/SCI");
    return $array;
}
function isProjectInApprovedState($status) {
    global $activeProjectStatusArray;
    for ($i = 0; $i < sizeof($activeProjectStatusArray); $i++) {
        if (strcmp($activeProjectStatusArray[$i], $status) === 0) {
            return true;
        }
    }
    return false;
}

function getAllCSSFileArray() {
    global $cssFilenameArray;
    return $cssFilenameArray;
}

function getAllJSFileArray() {
    global $jsFilenameArray;
    return $jsFilenameArray;
}

function getNewStatusFromButtonClick($buttonText) {
    global $projectButtonNewStatusArray;
    if (isset($projectButtonNewStatusArray[$buttonText])) {
        return $projectButtonNewStatusArray[$buttonText];
    } else {
        return NULL;
    }
}

function sendSignupRequest($message) {
    global $webmasterEmail;
    $subject = "METOC space portfolio signup request";
    return mail($webmasterEmail, $subject, $message);
}

function sendWebmasterMessage($subject, $message) {
    global $webmasterEmail;
    return mail($webmasterEmail, $subject, $message);
}

function getFYFromMysqlDateFormatStartDate($dateString) {
    //MYSQL DATE FORMAT: YYYY-MM-DD
    //DATEPICKER FORMAT: MM/DD/YYYY
    $pieces = explode("-", $dateString);
    $year = $pieces[0];
    $month = $pieces[1];
    if ($month >= 10) {
        return $year + 1;
    } else {
        return $year;
    }
}

function getTaskStatusArray() {
    global $taskStatusArray;
    return $taskStatusArray;
}

function getFYFromDatepickerFormat($dateString) {
    //MYSQL DATE FORMAT: YYYY-MM-DD
    //DATEPICKER FORMAT: MM/DD/YYYY
    $pieces = explode("/", $dateString);
    $year = $pieces[2];
    $month = $pieces[0];
    if ($month >= 10) {
        return $year + 1;
    } else {
        return $year;
    }
}

/**
 * 
 * @global array $projectStatusArray
 * @param type $dbStatus
 * @param type $type
 * @return string
 */
function getStatusHumanReadableString($dbStatus, $type) {
    if (strcmp($type, "project") === 0) {
        global $projectStatusArray;
        return $projectStatusArray[$dbStatus];
    } else if (strcmp($type, "task") === 0) {
        global $taskStatusArray;
        if (isset($taskStatusArray[$dbStatus])) {
            return $taskStatusArray[$dbStatus];
        } else {
            return $dbStatus; //grey
        }
    }
}

function getStatusBackgroundColor($dbStatus, $type) {
    if (strcmp($type, "project") === 0) {
        global $projectStatusArray;
        return $projectStatusArray[$dbStatus];
    } else if (strcmp($type, "task") === 0) {
        global $taskStatusColorArray;
        if (isset($taskStatusColorArray[$dbStatus])) {
            return $taskStatusColorArray[$dbStatus];
        } else {
            return "#e6ebe6"; //grey
        }
    }
}

/**
 * Returns a YYYY-MM-DD string from the MM/DD/YYYY input
 * @param type $dateString input in  MM/DD/YYYY format
 */
function getMysqlDateFormat($dateString) {
    //MYSQL DATE FORMAT: YYYY-MM-DD
    //DATEPICKER FORMAT: MM/DD/YYYY
    if (!strpos($dateString, "-")) {
        $pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
        $pieces = explode("/", $dateString);
        if (isset($pieces[0]) && isset($pieces[1]) && isset($pieces[2])) {
            return $pieces[2] . '-' . $pieces[0] . '-' . $pieces[1];
        } else {
            return "0000-00-00";
        }
    }
    //entered by the date calendar
    else {
        return $dateString;
    }
}

function getDatePickerFormat($dateString) {
    //if - is in the string assume the user entered it correctly
    //echo $dateString.'<br> Hello';
    if (!strpos($dateString, "/")) {
        $pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
        $pieces = explode("-", $dateString);
        return $pieces[1] . '/' . $pieces[2] . '/' . $pieces[0];
    }
    //entered by the date calendar
    else {
        return $dateString;
    }
}

function getCurrentFY() {
    $curDate = date("Y-m-d");
    return getFYFromMysqlDateFormatStartDate($curDate);
}

function isSringInArray($string, $array) {
    for ($i = 0; $i < sizeof($array); $i++) {
        if (strcmp($array[$i], $string) === 0) {
            return true;
        }
    }
    return false;
}

/**
 * Gets an array of fields that begin with $alpha
 * @param type $alpha the alpha part of the fieldname (eg. Fieldname = checkbox0, checkbox1... apha = checkbox)
 * @param type $length number of fields
 * @param type $returnIndices true if you want the indices of the clicked field not the value of it
 * @return array
 */
function getgenericList($alpha, $length, $returnIndices = NULL) {
    $array = array();
    //echo "returnIndices: $returnIndices";
    for ($i = 0; $i < $length; $i++) {
        $name = "$alpha$i";
        if (isset($_POST['' . $name . ''])) {
            if ($returnIndices) {
                $value = $i;
                //echo "i=$i";
            } else {
                $value = $_POST['' . $name . ''];
                //echo "val=$value <br>";
            }
            array_push($array, $value);
        }
    }
    return $array;
}

function getIndexClickedGenericList($alpha, $length) {
    for ($i = 0; $i < $length; $i++) {
        $name = "$alpha$i";
        if (isset($_POST['' . $name . ''])) {
            return $i;
        }
    }
    return -1;
}

function getIndexClickedValueGenericList($alpha, $length) {
    for ($i = 0; $i < $length; $i++) {
        $name = "$alpha$i";
        if (isset($_POST['' . $name . ''])) {
            return $_POST['' . $name . ''];
        }
    }
    return -1;
}

function fillArrayNoDuplicates($result) {
    $array = array();
    $duplicate = false;
    while ($row = mysqli_fetch_array($result)) {
        if (sizeof($array) === 0) {
            array_push($array, $row[0]);
        }
        for ($i = 0; $i < sizeof($array); $i++) {
            if (strcmp($array[$i], $row[0]) == 0) {
                $duplicate = true;
                break;
            }
        }
        if (!$duplicate) {
            array_push($array, $row[0]);
        }
        $duplicate = false;
    }
    return $array;
}

function getNextProjectWbsAvailable($fy) {
    $wbsNextLevel = 1;
    //query the task wbs level until we get a null result
    do {
        $testWbs = $wbsNextLevel . '.' . '0';
        $sqlSelect = "SELECT wbsString from wbs WHERE wbsString='$testWbs' and fy='$fy'";
        //echo $sqlSelect . '<br>';
        $wbsResult = executeSelect($sqlSelect);
        $wbsNextLevel++;
        //sleep(1);
    } while (!$wbsResult || ($wbsResult->num_rows !== 0));
    return $testWbs;
}

function getNextWbsAtThisLevel($wbsString) {
    $wbsNextLevel = 1;
    //query the task wbs level until we get a null result
    do {
        $testWbs = $wbsString . '.' . $wbsNextLevel;
        $sqlSelect = "SELECT wbsString from wbs WHERE wbsString='$testWbs'";
        //echo $sqlSelect . '<br>';
        $wbsResult = executeSelect($sqlSelect);
        $wbsNextLevel++;
        //sleep(1);
    } while (!$wbsResult || ($wbsResult->num_rows !== 0));
    return $testWbs;
}

/**
 * Swaps the level 1 wbs in $wbsString with the passed in $newLevel1Wbs (in:2.0.5 out:4.0.5)
 * @param type $wbsString the old wbs string (ex. 2.0.5)
 * @param type $newLevel1Wbs the new desired wbs level 1 (ex. 4)
 * @return type
 */
function swapLevel1Wbs($wbsString, $newLevel1Wbs) {

    //echo "swap--> old: $wbsString level: $newLevel1Wbs ";
    $endIndex = strpos($wbsString, ".");
    //echo " index:$endIndex ";
    $level1WbsRemoved = substr($wbsString, $endIndex + 1);
    $output = $newLevel1Wbs . '.' . $level1WbsRemoved;
    //echo "new: $output <br>";
    return $output;
}

?>
